<template>
  <Dialog
    :title="t('judgeScene.final.finalSettings')"
    v-model="dialogVisible"
    width="420"
    :fullscreen="false"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('scxFinalSettings.fields.finalDate')" prop="finalDate">
        <el-date-picker
          v-model="formData.finalDate"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('scxFinalSettings.fields.finalDate')"
        />
      </el-form-item>
    </el-form>
    <el-button text type="primary" :disabled="!formData.id" @click="handleFinalJudgeSelect">
      {{ t('scxFinalJugde.select.title') }}
    </el-button>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
  <ScxFinalJudgeSelect ref="scxFinalJudgeSelectRef" />
</template>
<script setup lang="ts">
import { ScxFinalSettingsApi, ScxFinalSettingsVO } from '@/api/awards/scxfinalsettings'
import { FormRules } from 'element-plus'
import ScxFinalJudgeSelect from '@/views/awards/scxfinaljugde/components/ScxFinalJudgeSelect.vue'

/** 师昌绪终评-设置 表单 */
defineOptions({ name: 'ScxFinalSettingsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  awardId: undefined,
  amountId: undefined,
  finalDate: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  awardId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxFinalSettings.fields.awardId'),
      trigger: 'blur'
    }
  ],
  amountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('scxFinalSettings.fields.amountId'),
      trigger: 'blur'
    }
  ],
  finalDate: [
    {
      required: true,
      message: t('common.selectTextPlaceholder') + t('scxFinalSettings.fields.finalDate'),
      trigger: 'change'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (awardsId: string) => {
  dialogVisible.value = true
  resetForm()
  formLoading.value = true
  try {
    const data = await ScxFinalSettingsApi.getScxFinalSettingsByAwardsId(awardsId)
    if (data) {
      // 修改
      formType.value = 'update'
      formData.value = data
    } else {
      // 新增
      formType.value = 'create'
      formData.value.awardId = awardsId as any
    }
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ScxFinalSettingsVO
    if (formType.value === 'create') {
      await ScxFinalSettingsApi.createScxFinalSettings(data)
      message.success(t('common.createSuccess'))
    } else {
      await ScxFinalSettingsApi.updateScxFinalSettings(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    awardId: undefined,
    amountId: undefined,
    finalDate: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

const scxFinalJudgeSelectRef = ref<InstanceType<typeof ScxFinalJudgeSelect>>()
const handleFinalJudgeSelect = () => {
  if (formData.value.id) {
    scxFinalJudgeSelectRef.value?.open(formData.value.id)
  }
}
</script>
